package com.lbs.jiepiaisi.activity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.LocationManager;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.webkit.GeolocationPermissions;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.LinearLayout;

import com.lbs.jiepiaisi.R;
import com.lbs.jiepiaisi.base.BaseActivity;

/**
 *
 * 时间：2018/1/4 10:42
 * 作用：呼叫代驾
 */
public class GetDriverActivity extends BaseActivity {

    private LinearLayout mLinView;

    private WebView mWebView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isTemplate = true;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_get_driver);
        titleView.setText("代驾");


    }

    @Override
    protected void onResume() {
        super.onResume();
        if(isSystemLocationEnable()){
            findView();
            setListener();
            loadData();
        }else {
            //    通过AlertDialog.Builder这个类来实例化我们的一个AlertDialog的对象
            AlertDialog.Builder builder = new AlertDialog.Builder(GetDriverActivity.this);
            //    设置Title的图标
            builder.setIcon(R.mipmap.logo_start);
            //    设置Title的内容
            builder.setTitle("开启GPS定位");
            //    设置Content来显示一个信息
            builder.setMessage("无法获取您当前位置，请开启GPS定位");
            //    设置一个PositiveButton
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialog, int which)
                {
                    Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    startActivity(intent);
                }
            });
            //    设置一个NegativeButton
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialog, int which)
                {
                    findView();
                    setListener();
                    loadData();
                }
            });
            //    显示出该对话框
            builder.show();
        }
    }

    public void findView(){
        mLinView = (LinearLayout)findViewById(R.id.lin_get_driver);
        mWebView = new WebView(getApplicationContext()) ;

        mWebView.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
                callback.invoke(origin, true, false);
                super.onGeolocationPermissionsShowPrompt(origin, callback);
            }
        });
        //声明WebSettings子类
        WebSettings webSettings = mWebView.getSettings();

        //如果访问的页面中要与Javascript交互，则webview必须设置支持Javascript
        webSettings.setJavaScriptEnabled(true);

        // //启用地理定位
        webSettings.setGeolocationEnabled(true);

        //设置自适应屏幕，两者合用
        webSettings.setUseWideViewPort(true); //将图片调整到适合webview的大小
        webSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小

        //缩放操作
        webSettings.setSupportZoom(false); //支持缩放，默认为true。是下面那个的前提。
        webSettings.setBuiltInZoomControls(false); //设置内置的缩放控件。若为false，则该WebView不可缩放
        webSettings.setDisplayZoomControls(true); //隐藏原生的缩放控件

        //其他细节操作
        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); //关闭webview中缓存
        webSettings.setAllowFileAccess(true); //设置可以访问文件
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true); //支持通过JS打开新窗口
        webSettings.setLoadsImagesAutomatically(true); //支持自动加载图片
        webSettings.setDefaultTextEncodingName("utf-8");//设置编码格式

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        mWebView.setLayoutParams(params);


        mLinView.addView(mWebView);


    }
    public void setListener(){

    }

    public void loadData(){
        mWebView.loadUrl("https://common.diditaxi.com.cn/general/webEntry?");
    }

    protected void onDestroy() {
        if (mWebView != null) {
            mWebView.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
            mWebView.clearHistory();
            ((ViewGroup) mWebView.getParent()).removeView(mWebView);
            mWebView.destroy();
            mWebView = null;
        }
        super.onDestroy();
    }

    private boolean isSystemLocationEnable() {
        LocationManager manager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        boolean gpsLocationEnable = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        boolean networkLocationEnable = manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        return gpsLocationEnable && networkLocationEnable;
    }
}